package DAO;

import java.awt.Font;
import java.awt.event.*;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import Main.DBMain;
import View.QueryFrame;

public class QueryByNo extends JFrame {
	private JPanel contentPane = new JPanel();
	private JTextField stuNoField = new JTextField();
	public static JTextArea showArea = new JTextArea(100, 100);
	private JLabel lab_Tip;
	private JButton btn_Exit;
	private JButton btn_Query;
	String stuNo;

	public QueryByNo() {
		setTitle("个人成绩");
		setSize(600, 200);
		setLocationRelativeTo(null);
		setContentPane(contentPane);
		contentPane.setLayout(null);

		lab_Tip = new JLabel("请输入你的学号:");
		lab_Tip.setFont(new Font("宋体", Font.PLAIN, 16));
		lab_Tip.setBounds(30, 10, 150, 40);

		stuNoField.setBounds(180, 15, 130, 30);
		stuNoField.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				stuNoField.setText("");
			}
		});
		/* 按键监听，回车查询 */
		stuNoField.addKeyListener(new KeyAdapter() {
			public void keyPressed(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER)
					try {
						query();
					} catch (SQLException e1) {
						e1.printStackTrace();
					}
			}
		});

		btn_Query = new JButton("查询");
		btn_Query.setFont(new Font("宋体", Font.BOLD, 16));
		btn_Query.setBounds(360, 10, 70, 40);
		/* 按钮监听，点击查询 */
		btn_Query.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					query();
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
		});

		showArea.setText("");
		showArea.setBounds(5, 65, 575, 90);

		btn_Exit = new JButton("关闭");
		btn_Exit.setFont(new Font("宋体", Font.BOLD, 16));
		;
		btn_Exit.setBounds(480, 10, 70, 40);
		btn_Exit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new QueryFrame();
				setVisible(false);
			}
		});

		contentPane.add(lab_Tip);
		contentPane.add(stuNoField);
		contentPane.add(showArea);
		contentPane.add(btn_Query);
		contentPane.add(btn_Exit);
		setVisible(true);
	}

	public void query() throws SQLException {
		java.sql.Connection conn = null;
		PreparedStatement pstmt;
		ResultSet rs;
		stuNo = stuNoField.getText();
		showArea.setText("");
		/* 学号不为空，进行查询 */
		if (!("".equals(stuNo))) {
			try {
				conn = DBMain.getConnection();
				String sql = "select * from stu_info where stuNo= '" + stuNo + "'";// 查询语句
				pstmt = conn.prepareStatement(sql);
				rs = pstmt.executeQuery(sql);// 接收查询到的对象
				ResultSetMetaData colName = rs.getMetaData();
				/* 展示查询到的信息 */
				if (rs.next()) {// 添加列名
					for (int i = 1; i < colName.getColumnCount(); i++) {
						showArea.append(colName.getColumnName(i) + "\t");
					}
					showArea.append("\n");
					for (int i = 1; i < colName.getColumnCount(); i++) {
						showArea.append(rs.getString(i) + "\t");
					}
					showArea.append("\n");
				} else {
					JOptionPane.showMessageDialog(null, "该学号不存在！请重新输入");
					stuNoField.setText("");
				}
			}catch (Exception e1) {
				e1.printStackTrace();
			}
		} else {
			JOptionPane.showMessageDialog(null, "请输入学号！");
		}
	}
}
